package com.sk.weichat.view;

import android.content.Context;
import android.support.annotation.IntDef;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sk.weichat.R;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author lzs
 * @date 2019/10/23
 * Email:343067508@qq.com
 **/
public class DynamicShortVideoControlView extends FrameLayout {

    public static final int pause = 0;
    public static final int playing = 1;
    public static final int stop = 2;
    public static final int end = 3;


    @IntDef({pause, playing, stop, end}) //限定为MAN,WOMEN
    @Target({
            ElementType.PARAMETER,
            ElementType.FIELD,
            ElementType.METHOD,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface VideoState {
    }

    /**
     * 设置播放状态
     *
     * @param videoState
     */
    public void setState(@VideoState int videoState) {
        if (videoState == pause) {
            ivPause.setVisibility(View.VISIBLE);
        } else if (videoState == playing) {
            ivPause.setVisibility(View.GONE);
        } else if (videoState == stop) {
            ivPause.setVisibility(View.VISIBLE);
        } else if (videoState == end) {

        }
    }



    public DynamicShortVideoControlView(Context context) {
        this(context, null);
    }

    public DynamicShortVideoControlView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DynamicShortVideoControlView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private ImageView ivPause;
    private ImageView ivVoice;

    private TextView tvTime;
    private ProgressBar loadingBar;
    private ProgressBar progressBar;//进度条

    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View root = LayoutInflater.from(context).inflate(R.layout.view_dynamic_video_controll_view, null);
        tvTime = root.findViewById(R.id.tv_time);
        ivVoice = root.findViewById(R.id.iv_open_voice);
        ivVoice.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (controller != null) {
                    controller.onVoiceClick((ImageView) v);
                }
            }
        });
        ivPause = root.findViewById(R.id.iv_pause);
        ivPause.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (controller != null) {
                    controller.onPauseClick((ImageView) v);
                }
            }
        });
        loadingBar = root.findViewById(R.id.loading_progress_bar);
        progressBar = root.findViewById(R.id.progressBar);
        addView(root);
    }

    public void setVoiceIconState(boolean isSilent){
        if (isSilent) {
            ivVoice.setImageResource(R.drawable.icon_voice_close);
        } else {
            ivVoice.setImageResource(R.drawable.icon_voice_open);
        }
    }


    public void setPauseVisibilty(int visibilty) {
        ivPause.setVisibility(visibilty);
    }

    private VideoViewController controller;

    public VideoViewController getController() {
        return controller;
    }

    public void setController(VideoViewController controller) {
        this.controller = controller;
    }


//    public void setProgressBarVisibility(int visibility) {
//        progressBar.setVisibility(visibility);
//    }

    public void setTime(String time) {
        tvTime.setText(time);
    }

    public void updateProgress(long max, long current) {
        progressBar.setMax((int)max);
        progressBar.setProgress((int)current);
    }

    public void showLoading() {
        loadingBar.setVisibility(VISIBLE);
    }

    public void hideLoading() {
        loadingBar.setVisibility(GONE);
    }

    public interface VideoViewController {
        void onVoiceClick(ImageView imageView);

        void onPauseClick(ImageView imageView);

    }
}
